package cn.lzd.mr.TycMember;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created by lzd on 2018/8/21.
 */
public class TycCount {

    public static class MyMapper extends Mapper<Object, Text, Text, NullWritable> {


        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            try {
                String val = value.toString();
                JSONObject jsonObject = JSONObject.parseObject(val);
                String tmInfoCount = jsonObject.getString("tmInfoCount");
                if (StringUtils.isNotBlank(tmInfoCount) && (!"0".equals(tmInfoCount))) {
                    context.write(value, NullWritable.get());
                }
            }catch (Exception e){
                System.out.println("==================="+value.toString());
            }

        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        job.setJarByClass(TycCount.class);

        job.setMapperClass(TycCount.MyMapper.class);



        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

//        FileInputFormat.setMaxInputSplitSize(job,131072);//128M
//        FileInputFormat.setMinInputSplitSize(job,524288);//512M
//33649
        FileInputFormat.setMaxInputSplitSize(job,524288);//512
        FileInputFormat.setMinInputSplitSize(job,223232);//218


        FileInputFormat.setInputPaths(job, new Path("/D:/1688/2045w_zdl"));
        FileOutputFormat.setOutputPath(job, new Path("/D:/1688/out/"));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
